package com.platform.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.CommonwealEntity;
import com.platform.service.CommonwealService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 公益活动表Controller
 *
 * @author czj
 * @email 729618941@qq.com
 * @date 2018-10-31 15:32:20
 */
@RestController
@RequestMapping("commonweal")
public class CommonwealController {
    @Autowired
    private CommonwealService commonwealService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("commonweal:list")
    public R list(@RequestParam Map<String, Object> params) {

        //仅显示没有删除的公益活动
        params.put("isDelete", 0);

        //查询列表数据
        Query query = new Query(params);

        List<CommonwealEntity> commonwealList = commonwealService.queryList(query);
        if (commonwealList.size() > 0) {
            for (CommonwealEntity commonwealEntity : commonwealList) {
                commonwealEntity.setWxAppLink("../publicDetail/publicDetail?commonwealId=" + commonwealEntity.getId());
            }
        }
        int total = commonwealService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(commonwealList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("commonweal:info")
    public R info(@PathVariable("id") Integer id) {
        CommonwealEntity commonweal = commonwealService.queryObject(id);

        return R.ok().put("commonweal", commonweal);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("commonweal:save")
    public R save(@RequestBody CommonwealEntity commonweal) {
        String tags = commonweal.getTags();
        if (tags != null && tags.contains("，")) {
            return R.error("多个标签请使用英文逗号隔开");
        }

        commonweal.setAddTime(new Date());
        commonweal.setLoveCount(0);
        commonweal.setTotalFundraising(new BigDecimal(0));
        commonwealService.save(commonweal);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("commonweal:update")
    public R update(@RequestBody CommonwealEntity commonweal) {
        String tags = commonweal.getTags();
        if (tags != null && tags.contains("，")) {
            return R.error("多个标签请使用英文逗号隔开");
        }
        commonwealService.update(commonweal);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("commonweal:delete")
    public R delete(@RequestBody Integer[] ids) {
        for (Integer id : ids) {
            CommonwealEntity commonwealEntity = commonwealService.queryObject(id);
            commonwealEntity.setIsDelete(1);
            commonwealService.update(commonwealEntity);
        }
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<CommonwealEntity> list = commonwealService.queryList(params);

        return R.ok().put("list", list);
    }
}
